package yxy.game.pm2.module.player;

import java.util.Collection;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import yxy.cherry.battle.Battle;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.playerlvcost;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.*;
import yxy.game.pm2.bean.Format.FormatType;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.reset.ExperienceReset;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.message.PlayerMessage;
import yxy.game.pm2.module.hero.FormatHandle;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;

import static yxy.game.pm2.bean.currency.CurrencyType.CT_主公经验;

public class PlayerHandle {

    public PlayerMessage.PlayerInfo playerInfo(PlayerBean player) {
        String playerCode = player.getCode();
        FormatHandle formatModule = new FormatHandle();
        Format format = formatModule.get(playerCode, FormatType.普通);
        Collection<Hero> formatHeros = formatModule.formatHeros(playerCode, FormatType.普通).values();

        PlayerMessage.PlayerInfo.Builder builder = PlayerMessage.PlayerInfo.newBuilder();
        builder.setServerId(player.getServerId());
        builder.setPlayerCode(player.getCode());
        builder.setNickname(player.getName());
        builder.setHead(player.getHead());
        builder.setHeadFrame(player.getHeadframe());
        builder.setAvatar(player.getAvatar());
        builder.setTitle(player.getTitle());
        builder.setLevel(player.getLevel());

        builder.setExp(Currency.number(playerCode, CurrencyType.CT_主公经验, 0,false));
        builder.setFormat(formatModule.get(format));
        HeroHandle heroModule = new HeroHandle();
        for (Hero hero : formatHeros) {
            try {
                HeroMessage.HeroSimple heroSimple = heroModule.getHeroSimple(playerCode, hero);
                builder.addHeros(heroSimple);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        builder.setPower(player.getPower());
        builder.setVip(player.getViplevel());

        builder.setRegion(player.getTown());
        builder.setCity(player.getCity());

        // 联盟名判断
        UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
        Result result = new UnionHandle().checkPlayerUnion(entry);
        if (result.equals(Result.UnionIn)) {
            UnionBean union = entry.getUnionBean();
            builder.setUnion(union.getName());
        }

        return builder.build();
    }

    public PlayerMessage.PlayerSimpleInfo.Builder playerSimpleInfo(String playerCode) {
        PlayerBean player = PlayerBean.get(playerCode);
        return playerSimpleInfo(player);
    }

    public PlayerMessage.PlayerSimpleInfo.Builder playerSimpleInfo(PlayerBean player) {
        PlayerMessage.PlayerSimpleInfo.Builder builder = PlayerMessage.PlayerSimpleInfo.newBuilder();
        builder.setServerId(player.getServerId());
        builder.setPlayerCode(player.getCode());
        builder.setNickname(player.getName());
        builder.setHead(player.getHead());
        builder.setHeadFrame(player.getHeadframe());
        builder.setAvatar(player.getAvatar());
        builder.setLevel(player.getLevel());
        builder.setPower(player.getPower());
        builder.setVip(player.getViplevel());
        builder.setTitle(player.getTitle());
        return builder;
    }

    public PlayerMessage.PlayerSimpleInfo.Builder playerChatInfo(PlayerBean player) {
        PlayerMessage.PlayerSimpleInfo.Builder builder = PlayerMessage.PlayerSimpleInfo.newBuilder();
        builder.setServerId(player.getServerId());
        builder.setPlayerCode(player.getCode());
        builder.setNickname(player.getName());
        builder.setHead(player.getHead());
        builder.setHeadFrame(player.getHeadframe());
        builder.setAvatar(player.getAvatar());
        builder.setTitle(player.getTitle());
        builder.setLevel(player.getLevel());
        builder.setPower(player.getPower());
        builder.setVip(player.getViplevel());
        builder.setRegion(player.getTown());
        builder.setCity(player.getCity());
        return builder;
    }


    //2022.4.8新增每个用户当前等级的经验30~70%随机初始化
    public void changeExperience(PlayerBean player, String playerCode) {
        String isChangeExp = ExperienceReset.get(playerCode);
        if (StringUtils.isEmpty(isChangeExp) && player.getLevel() != 1) {
            Battle.battleLogger(1, "--------------兼容处理:每个用户当前等级的经验30~70%随机初始化---------------");
            int level = player.getLevel();//玩家当前等级
            playerlvcost _playerlvcost = DataCenter.getData(level, playerlvcost.class);//升级表
            long costnum = _playerlvcost.getCostnum();//升级需要花费的经验
            long round = Math.round(new Random().nextDouble() * (costnum * 0.4) + costnum * 0.3);//四舍五入30%~70%
            Point.set(playerCode, CT_主公经验, round);
            ExperienceReset.set(playerCode, "1");
        }
    }

}
